home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / dbu / secupdate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-02-08  |  4.2 KB  |  183 lines

  1. # include    <ingres.h>
  2. # include    <aux.h>
  3. # include    <catalog.h>
  4. # include    <symbol.h>
  5. # include    <access.h>
  6. # include    <batch.h>
  7. # include    <sccs.h>
  8.  
  9. SCCSID(@(#)secupdate.c    8.3    2/8/85)
  10.  
  11. /*
  12. **     SECUPDATE - updates secondary indexes
  13. **
  14. **
  15. **    Parameters:
  16. **        rel - relation being updated
  17. **
  18. **    Return Codes:
  19. **        0
  20. **
  21. **    Trace Flags:
  22. **        Z49, 49.7 49.8 49.15
  23. **
  24. **    Called by:
  25. **        update()
  26. **
  27. */
  28. secupdate(r)
  29. register DESC    *r;
  30. {
  31.     register char    *p;
  32.     register int    i;
  33.     int        j, domcnt, mode, dom;
  34.     long        tupcnt;
  35.     long        oldtid, newtid;
  36.     long        lotid, hitid, uptid;
  37.     char        oldtup[MAXTUP], newtup[MAXTUP];
  38.     char        oldkey[MAXTUP], newkey[MAXTUP];
  39.     char        dumtup[MAXTUP];
  40.     struct index    itup;
  41.     DESC        si_desc;
  42.     extern DESC    Inddes;
  43.     struct key_pt
  44.     {
  45.         char    *pt_old;
  46.         char    *pt_new;
  47.     };
  48.     struct key_pt    keys[MAXKEYS+1];
  49.  
  50.     mode = Batchhd.mode_up;
  51.     Batch_dirty = FALSE;
  52. #    ifdef xZTR1
  53.     if (tTf(49, -1))
  54.         printf("SECUPDATE\n");
  55. #    endif
  56.     opencatalog("indexes", OR_READ);
  57.     setkey(&Inddes, &itup, r->reldum.relid, IRELIDP);
  58.     setkey(&Inddes, &itup, r->reldum.relowner, IOWNERP);
  59.     if (i = find(&Inddes, EXACTKEY, &lotid, &hitid, &itup))
  60.         syserr("secupdate:find indexes %d", i);
  61.  
  62.     /* update each secondary index */
  63.     while(!(i = get(&Inddes, &lotid, &hitid, &itup, TRUE)))
  64.     {
  65.         /* check if the index is on the right relation */
  66. #        ifdef xZTR1
  67.         if (tTf(49, 7))
  68.             printup(&Inddes, &itup);
  69. #        endif
  70.         if (!bequal(itup.irelidp, r->reldum.relid, MAXNAME) ||
  71.             !bequal(itup.iownerp, r->reldum.relowner, 2))
  72.             continue;
  73.  
  74.         if (i = openr(&si_desc, OR_WRITE, itup.irelidi))
  75.             syserr("secupdate:can't openr %.12s %d", itup.irelidi, i);
  76.         /* reposition batch file to the beginning. */
  77.         if ((i = lseek(Batch_fp, 0L, 0)) < 0)
  78.             syserr("secupdate:seek %d %d", i, Batch_fp);
  79.         Batch_cnt = BATCHSIZE;
  80.         getbatch(&Batchhd, sizeof Batchhd);    /* reread header */
  81.  
  82.         /* set up the key structure */
  83.         p = itup.idom;
  84.         for (domcnt = 0; domcnt < MAXKEYS; domcnt++)
  85.         {
  86.             if ((dom = *p++) == 0)
  87.                 break;    /* no more key domains */
  88. #            ifdef xZTR1
  89.             if (tTf(49, 15))
  90.                 
  91.                 printf("dom %d,tupo_off %d\n", dom, Batchhd.si[dom].tupo_off);
  92. #            endif
  93.             keys[domcnt].pt_old = &oldtup[Batchhd.si[dom].tupo_off];
  94.             keys[domcnt].pt_new = &newtup[r->reloff[dom]];
  95.         }
  96.  
  97.         /* the last domain is the "tidp" field */
  98.         keys[domcnt].pt_old = (char *) &oldtid;
  99.         keys[domcnt].pt_new = (char *) &newtid;
  100.  
  101.         /*
  102.         ** Start reading the batch file and updating
  103.         ** the secondary indexes.
  104.         */
  105.         tupcnt = Batchhd.num_updts;
  106.         while (tupcnt--)
  107.         {
  108.             getbatch(&oldtid, Batchhd.tido_size);
  109.             getbatch(oldtup, Batchhd.tupo_size);
  110.             getbatch(newtup, Batchhd.tupn_size);
  111.             getbatch(&newtid, Batchhd.tidn_size);
  112.  
  113.             /* if this is a replace or append form the new key */
  114.             if (mode != mdDEL)
  115.             {
  116.                 for (j = 0; j <= domcnt; j++)
  117.                     setkey(&si_desc, newkey, keys[j].pt_new, j+1);
  118. #                ifdef xZTR1
  119.                 if (tTf(49, 7))
  120.                     printup(&si_desc, newkey);
  121. #                endif
  122.             }
  123.  
  124.             /* if this is delete or replace form the old key */
  125.             if (mode != mdAPP)
  126.             {
  127.                 for (j = 0; j <= domcnt; j++)
  128.                     setkey(&si_desc, oldkey, keys[j].pt_old, j+1);
  129. #                ifdef xZTR1
  130.                 if (tTf(49, 8))
  131.                     printup(&si_desc, oldkey);
  132. #                endif
  133.             }
  134.  
  135.             switch (mode)
  136.             {
  137.  
  138.               case mdDEL:
  139.                 if (i = getequal(&si_desc, oldkey, dumtup, &uptid))
  140.                 {
  141.                     if (i > 0)
  142.                         break;
  143.                     syserr("secupdate:getequal %d", i);
  144.                 }
  145.                 if ((i = delete(&si_desc, &uptid)) < 0)
  146.                     syserr("secupdate:delete %d", i);
  147.                 break;
  148.  
  149.               case mdREPL:
  150.                 /* if the newtup = oldtup then do nothing */
  151.                 if (bequal(oldkey, newkey, si_desc.reldum.relwid))
  152.                     break;
  153.                 if (i = getequal(&si_desc, oldkey, dumtup, &uptid))
  154.                 {
  155.                     if (Batch_recovery && i > 0)
  156.                         goto secinsert;
  157.                     syserr("secupdate:getequal-repl %d", i);
  158.                 }
  159.                 if (i = replace(&si_desc, &uptid, newkey, TRUE))
  160.                 {
  161.                     /* if newtuple is dup of old, ok */
  162.                     if (i == 1)
  163.                         break;
  164.                     /* if this is recovery and old tid not there, try an insert */
  165.                     if (Batch_recovery && i == 2)
  166.                         goto secinsert;
  167.                     syserr("secupdate:replace %d", i);
  168.                 }
  169.                 break;
  170.  
  171.               case mdAPP:
  172.               secinsert:
  173.                 if ((i = insert(&si_desc, &uptid, newkey, TRUE)) < 0)
  174.                     syserr("secupdate:insert %d", i);
  175.             }
  176.         }
  177.         if (i = closer(&si_desc))
  178.             syserr("secupdate:closer %.12s %d", si_desc.reldum.relid, i);
  179.     }
  180.     if (i < 0)
  181.         syserr("secupdate:bad get from indexes %d", i);
  182. }
  183.